<?php
/**
 * User Model
 *
 * @package  CodeIgniter
 * @categroy models
 * @author   Oneplus <oneplus.lau@gmail.com>
 * @link     sewiki.oneplus.info
 */
class User extends CI_Model {
	function __construct() {
		parent::__construct();
		$this->load->database();
	}
	
	public function login($username, $password) {
		$user = $this->db->get_where('users', array(
			'username' => $username,
			'password' => md5($password)));
		
		if($user->num_rows() == 1) {
			$row  = $user->row();

			$data = array(
				'uid'  => $row->uid,
				'role' => $row->role);
			
			return $data;
		}
		else {
			return array();
		}
	}
	
	public function logout() {
		$this->session->destory();
	}
	
	public function createUser($data) {
		$this->db->insert('users', $data);
		return $this->db->insert_id();
	}
	/**
	 *
	 */
	public function getUser($uid) {
		$query = $this->db->get_where('users', array('uid' => $uid));
		$user  = array(
			'uid'      => $query->row()->uid,
			'username' => $query->row()->username,
			'email'    => $query->row()->email);
		
		return $user;
	}
	public function createUserInterest($uid, $interests) {
		foreach($interests as $interest) {
			$data = array(
				'uid'      => $uid,
				'interest' => $interest);
			$this->db->insert('user_interests', $data);
		}
	}
	/**
	 * Function existUser, judge whether the user
	 * with username of $usr exist
	 *
	 * @access public
	 * @param  string, usr
	 * @return bool
	 */
	public function existUser($username) {
		$usr = $this->db->get_where('users', array('username' => $username));
		return ($usr->num_rows() == 1);
	}
	/**
	 * Function existEmail, judge whether the user
	 * with email of $usr exist
	 *
	 * @access public
	 * @param  string, usr
	 * @return bool
	 */
	public function existEmail($email) {
		$email = $this->db->get_where('users', array('email' => $email));
		return ($email->num_rows() == 1);
	}
	/**
	 * Function getRole, get the role of current user
	 *
	 * @access public
	 * @return int
	 */
	public function role() {
		return $this->session->userdata('role');
	}
	/**
	 * Function isEditorForBook, judge user uid in
	 * play list of book bid
	 *
	 * @access public
	 * @param  uid, int
	 * @param  bid, int
	 * @return bool
	 */
	public function isEditorForBook($uid, $bid) {
		$query = $this->db->get_where('editor_list', array('uid' => $uid, 'bid' => $bid));
		return ($query->num_rows() == 1);
	}
	/**
	 * Function isAdminForBook, judge user uid in
	 * play list of book bid
	 *
	 * @access public
	 * @param  uid, int
	 * @param  bid, int
	 * @return bool
	 */
	public function isAdminForBook($uid, $bid) {
		$query = $this->db->get_where('books', array('bid' => $bid));
		return ($query->row()->uid == $uid);
	}
	/**
	 *
	 *
	 *
	 *
	 */
	public function isAdminForChapter($uid, $cid) {
		$query = $this->db->get_where('chapters', array('cid' => $cid));
		$bid   = $query->row()->bid;
		return $this->isAdminForBook($uid, $bid);
	}
	/**
	 * Function isOwnerOfThread, judge user uid is
	 * the owner of a thread
	 *
	 * @access public
	 * @param  uid, int
	 * @param  tid, int
	 * @return bool
	 */
	public function isOwnerOfThread($uid, $tid) {
		$query = $this->db->get_where('threads', array('tid' => $tid));
		return ($query->row()->uid == $uid);
	}
	/**
	 * Function isOwnerOfPoster, judge user uid is
	 * the owner of a thread
	 *
	 * @access public
	 * @param  uid, int
	 * @param  pid, int
	 * @return bool
	 */	
	public function isOwnerOfPoster($uid, $pid) {
		$query = $this->db->get_where('posters', array('pid' => $pid));
		return ($query->row()->uid == $uid);
	}
	/**
	 * Function isOwnerOfEdition, judge user uid is
	 * the owner of a edition
	 *
	 * @access public
	 * @param  uid, int
	 * @param  pid, int
	 * @return bool
	 */
	public function isOwnerOfEdition($uid, $eid) {
		$query = $this->db->get_where('editions', array('eid' => $eid));
		return ($query->row()->uid == $uid);
	}
	/**
	 *
	 *
	 *
	 */
	public function isAdminForThread($uid, $tid) {
		$query = $this->db->get_where('threads', array('tid' => $tid));
		$cid   = $query->row()->cid;
		return $this->isAdminForChapter($uid, $cid);
	}
	/**
	 *
	 *
	 *
	 */
	public function isAdminForPoster($uid, $pid) {
		$query = $this->db->get_where('posters', array('pid' => $pid));
		$tid   = $query->row()->tid;
		return $this->isAdminForThread($uid, $tid);
	}
}
?>